import os
from subprocess import Popen, PIPE


def test_wc1():
    # 生成测试数据
    fname = '/tmp/test_data'
    f = open(fname, 'w')
    f.write('aaa\n')
    f.write('bbb\n')
    f.close()
    p = Popen(['wc', '-l', fname], stdout=PIPE)
    text = p.stdout.read()
    total = int(text.split()[0])
    os.remove(fname)
    assert total == 2


def test_wc2():
    # 生成测试数据
    fname = '/tmp/test_data'
    f = open(fname, 'w')
    f.write('aaa\n')
    f.write('bbb')
    f.close()
    p = Popen(['wc', '-l', fname], stdout=PIPE)
    text = p.stdout.read()
    total = int(text.split()[0])
    os.remove(fname)
    assert total == 2


if __name__ == '__main__':
    ok = 0
    failed = 0
    for tester in [test_wc1, test_wc2]:
        try:
            tester()
        except AssertionError:
            failed += 1
        else:
            ok += 1
    print('total run: %s, success: %s, failed: %s' % (ok+failed, ok, failed))
